// pages/login/login.js
var app = getApp();

Page({
  data: {
    inputValue: '',
    second: 60,
    pagePath: "",
    verifyCode: "",
    addImgDis: "none",
    addGoldImg: "",
    date: ["1990-01-01"],
    phoneNumber: "",
    code1: true,
    code2: false,
    vip_id: null,
    isClick: true,
  },
  onLoad: function (options) {
    console.log("页面传递的", options)
    var that = this;
    wx.showShareMenu({
      withShareTicket: true
    })
    var pagePath = options.pagePath;
    var page = options.pages
    var vip_id = wx.getStorageSync("vip_id")
    if(vip_id==''||vip_id==null||vip_id==0){
      let that = this;
      wx.request({
        url: app.urlData.getURL + '/index.php/api/SApi/index/key/' + app.urlData.key + '/m/is_customerid/wid/' + app.urlData.wid + '/sign/' + wx.getStorageSync('sign'),
        method: 'get',
        success(res) {
          console.log("读取vip_id", res)
          that.setData({
            vip_id: res.data.vip_id,
            pagePath: pagePath,
            phoneNumber: options.number
          })
          if (res.data.vip_id != 0 && res.data.vip_id != '') {
            wx.setStorageSync('vip_id', res.data.vip_id);
          }
        }
      })
    }else{
      that.setData({
        pagePath: pagePath,
        vip_id: vip_id,
        phoneNumber: options.number
      })
    }

  },

  // 选择生日

  bindDateChange: function (e) {
    let that = this;
    that.setData({
      date: e.detail.value
    })
  },

  bindPhoneInput: function (e) {
    let that = this;
    that.setData({
      phoneNumber: e.detail.value
    })
  },

  bindKeyInput: function (e) {
    let that = this;
    that.setData({
      verifyCode: e.detail.value
    })
  },

  // 获取手机验证码
  // getPhoneCode: function () {
  //   var that = this;
  //   var mobile = that.data.phoneNumber;
  //   console.log("获取手机号", mobile)
  //   if (!mobile) {
  //     wx.showToast({
  //       title: '请填写正确手机号码',
  //       image: '../../images/tip_icon_warn.png'
  //     });
  //   } else {
  //     if (that.data.isClick == true) {
  //       that.setData({
  //         isClick: false
  //       });
  //       wx.request({
  //         url: app.url1 + "/hpl/index.php?s=/Api/hfzx/index",
  //         method: "POST",
  //         data: {
  //           key: "be15d4ca913c91494cb4f9cd6ce317c6",
  //           type: "send-code",
  //           data: {
  //             'origin': 'hplgwsc',
  //             'mobile': mobile,
  //             'vip_id': wx.getStorageSync("vip_id")
  //           }
  //         },
  //         success: function (res) {
  //           console.log(res)
  //           var second = that.data.second;
  //           if (res.data.status === 1) {
  //             wx.showToast({
  //               title: '验证码发送成功',
  //               icon: 'success',
  //               duration: 1000
  //             })
  //             that.setData({
  //               code1: false,
  //               code2: true,
  //               loginStatic: true
  //             });
  //             var time = setInterval(function () {
  //               if (second > 0) {
  //                 second = second - 1;
  //                 that.setData({
  //                   second: second
  //                 });
  //               } else {
  //                 that.setData({
  //                   code1: true,
  //                   code2: false,
  //                   second: 60,
  //                 })
  //                 clearInterval(time);
  //               }
  //             }, 1000);
  //           } else {
  //             wx.showToast({
  //               title: res.data.msg,
  //               icon: 'success',
  //               duration: 1000
  //             })
  //           }
  //           that.setData({
  //             isClick: true
  //           });
  //         }
  //       })
  //     }
  //   }
  // },



  // 注册
  loginIn: function (res) {
    console.log(res, mobile);
    let that = this;
    let mobile = that.data.phoneNumber;
    let date = that.data.date;
    if (!mobile) {
      wx.showToast({
        title: '请填写正确手机号码',
        icon: 'success',
        duration: 1000
      });
      return false;
    } else if (!date) {
      wx.showToast({
        title: '请填写生日日期',
        icon: 'success',
        duration: 1000
      });
      return false;
    }
    if (wx.getStorageSync('is_ceshi')!= 1){
      var url_type = 0;//为0为正式的注册
    }else{
      var url_type = 1;//为1为测试的注册
    }
    wx.request({
      url: app.url1 + "/hpl/index.php?s=/Api/hfzx/index",
      method: "POST",
      data: {
        key: "be15d4ca913c91494cb4f9cd6ce317c6",
        type: "register",
        data: {
          mobile: mobile,
          // code: that.data.verifyCode,
          birth: that.data.date,
          register_type: 2,
          'vip_id': that.data.vip_id,
          'origin': 'hplgwsc',
          origin_from:1,
          url_type: url_type,
          hpl_uid:wx.getStorageSync('ydn_id')
        }
      },
      success(res) {
        console.log(res, getCurrentPages());
        console.log(res.data.status, "验证状态1111111")
        if (res.data.status != 0 ) {
          if (res.data.data.customerid!=null){
            wx.setStorageSync('customerid', res.data.data.customerid)
            wx.request({
              url: app.urlData.getURL + '/index.php/api/SApi/index/key/' + app.urlData.key + '/m/updateShareId/wid/' + app.urlData.wid + '/sign/' + wx.getStorageSync('sign'),
              method:"get",
              data:{
                share_id: wx.getStorageSync('share_id')
              },
              success:function(){
              }
            })
          }
          that.customer_id();
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 1000
          });
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/index/index',
            });
          }, 600)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1000
          });
        }
      }
    })
  },
  customer_id:function(){
    wx.request({
      url: app.urlData.getURL + '/index.php/api/SApi/index/key/' + app.urlData.key + '/m/is_customerid/wid/' + app.urlData.wid + '/sign/' + wx.getStorageSync('sign'),
      method: 'GET',
      success: (res) => {
        if (res.data.vip_id != 0 && res.data.vip_id != '') {
          wx.setStorageSync('vip_id', res.data.vip_id);
        }
        wx.setStorageSync('customerid', res.data.customerid);
      }
    })
  },

})
